//package keter.web.servlet.wx;
//
//import java.io.IOException;
//
//import javax.servlet.ServletException;
//import javax.servlet.http.HttpServlet;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//
//import org.slf4j.Logger;
//import org.slf4j.LoggerFactory;
//import org.springframework.http.MediaType;
//
//import com.jfinal.kit.HttpKit;
//import com.jfinal.kit.PropKit;
//import com.jfinal.weixin.sdk.api.ApiConfig;
//import com.jfinal.weixin.sdk.api.ApiConfigKit;
//import com.jfinal.weixin.sdk.kit.SignatureCheckKit;
//import com.jfinal.weixin.sdk.msg.InMsgParser;
//import com.jfinal.weixin.sdk.msg.OutMsgXmlBuilder;
//import com.jfinal.weixin.sdk.msg.in.InMsg;
//import com.jfinal.weixin.sdk.msg.in.InTextMsg;
//import com.jfinal.weixin.sdk.msg.out.OutTextMsg;
//
//import keter.framework.web.util.ResponseUtil;
//
//public class WxServlet extends HttpServlet {
//	/**
//	 * Logger for this class
//	 */
//	private static final Logger logger = LoggerFactory.getLogger(WxServlet.class);
//		
//	private void setConfig() {
//		ApiConfig ac = new ApiConfig();
//		ac.setToken(PropKit.get("token"));
//		ac.setAppId(PropKit.get("appId"));
//		ac.setAppSecret(PropKit.get("appSecret"));
//		ApiConfigKit.setThreadLocalApiConfig(ac);
//	}
//	
//	public void doGet(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		logger.info("-------验证请求是否来自微信！");		
//		response.setCharacterEncoding("UTF-8");
//		if(checkSignature(request)){
//			ResponseUtil.printHtml(request.getParameter("echostr"), response);
//		}
//	    response.setContentType(MediaType.APPLICATION_JSON.toString());
//	    	ResponseUtil.printHtml("滚",response);
//	}
//	
//	private boolean checkSignature(HttpServletRequest request){
//		//微信加密签名
//		String signature =request.getParameter("signature");
//		//时间戳 
//		String timestamp=request.getParameter("timestamp");
//		//随机数
//		String nonce=request.getParameter("nonce");
//		//随机字符串
//		String echostr=request.getParameter("echostr");	
//		if(signature==null||timestamp==null||nonce==null){
//			return false;
//		}
//
//		logger.info("---接收到的来自"+request.getRemoteHost()+",请求参数：signature:"+signature+",timestamp:"+timestamp+",nonce:"+nonce+",echostr:"+echostr);
//		setConfig();
//		return SignatureCheckKit.me.checkSignature(signature, timestamp, nonce);
//	}
//	
//
//	public void doPost(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//    	logger.info(request.getParameterNames()+"");
//		checkSignature(request);
//		//将请求、响应的编码均设置为UTF-8（防止中文乱码）   
//		request.setCharacterEncoding("UTF-8");
//		InMsg im = getInMsg(request);
//		if (im instanceof InTextMsg)
//			processInTextMsg((InTextMsg) im,response);
//	}
//	
//	protected void processInTextMsg(InTextMsg im, HttpServletResponse response){
//		//转发给多客服PC客户端
//		OutTextMsg outCustomMsg = new OutTextMsg(im);
//		outCustomMsg.setToUserName(im.getFromUserName());
//		outCustomMsg.setFromUserName(im.getToUserName());
//		outCustomMsg.setCreateTime(outCustomMsg.now());
//		outCustomMsg.setContent("你的消息是："+im.getContent());
//		String xml = OutMsgXmlBuilder.build(outCustomMsg).replace("\n", "");
//		logger.info("respose is: \n"+xml);
//		ResponseUtil.printHtml(xml,response);
//	}
//	
//	public String getInMsgXml(HttpServletRequest request) {
//		return HttpKit.readIncommingRequestData(request);
//	}
//	
//	public InMsg getInMsg(HttpServletRequest request) {
//		return InMsgParser.parse(getInMsgXml(request));
//	}
//}